import request from '@/utils/request'

// 用户相关API
const user = {
  // 获取用户信息
  getInfo() {
    return request.get('/api/user/info')
  },
  
  // 更新头像
  async updateAvatar(filePath) {
    try {
      // 构建完整的上传URL
      const uploadUrl = request.baseURL + '/api/avatar/upload'
      
      // 上传文件
      return new Promise((resolve, reject) => {
        uni.uploadFile({
          url: uploadUrl,
          filePath: filePath,
          name: 'file',
          formData: {}, // 添加空的formData对象
          header: {
            'Authorization': `Bearer ${uni.getStorageSync('token')}`
          },
          success: (uploadRes) => {
            try {
              const data = JSON.parse(uploadRes.data)
              if (data.code !== 200) {
                reject(new Error(data.message || '上传失败'))
              } else {
                resolve(data.data)
              }
            } catch (error) {
              reject(new Error('解析响应数据失败'))
            }
          },
          fail: (error) => {
            reject(error)
          }
        })
      })
    } catch (error) {
      throw error
    }
  },
  
  // 修改密码
  changePassword(data) {
    return request.post('/api/user/password', data)
  },
  
  // 获取年度工资统计(从春节开始)
  getYearSalary() {
    return request.get('/api/user/year-salary')
  },
  
  // 登出
  logout() {
    return request.post('/api/auth/logout')
  }
}

export default user 